package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class AddressBookServiceImpl implements AddressBookService {

    @Autowired
    private AddressBookMapper addressBookMapper;

    /**
     * 添加地址簿
     *
     * @param addressBook
     */
    public void add(AddressBook addressBook) {
        //获取当前登录用户id
        Long userId = BaseContext.getCurrentId();
        addressBook.setUserId(userId);
        addressBookMapper.insert(addressBook);
    }

    /**
     * 动态查询用户地址
     *
     * @return
     */
    public List<AddressBook> list(int type) {
        Long userId = BaseContext.getCurrentId();//获取当前登录用户id
        log.info("登录用户id:{}进行地址信息查询", userId);
        AddressBook addressBook = new AddressBook();

        //进行类型判断
        if (type == -1) {
            log.info("进行所有地址信息查询");
            addressBook.setUserId(userId);
            List<AddressBook> list = addressBookMapper.list(addressBook);
            return list;
        } else if (type == -2) {
            log.info("根据用户默认地址查询");
            addressBook.setUserId(userId);
            addressBook.setIsDefault(1);
            List<AddressBook> list = addressBookMapper.list(addressBook);
            return list;
        }
        log.info("根据id主键查询地址");
        Long id = (long) type;
        addressBook.setId(id);
        List<AddressBook> list = addressBookMapper.list(addressBook);
        return list;
    }

    /**
     * 根据id动态修改用户信息
     *
     * @param addressBook
     */
    @Transactional//事务管理
    public void modifyById(AddressBook addressBook) {
        //获取当前线程登录用户id
        Long userId = BaseContext.getCurrentId();
        //先对表进行删除操作，再进行插入新数据的操作
        Long id = addressBook.getId();//获得当前主键id
        AddressBook address = new AddressBook();
        address.setId(id);
        log.info("用户id:{}进行删除操作", userId);
        addressBookMapper.deleteById(address);
        //然后再对表进行插入
        log.info("用户id:{}进行插入操作", userId);
        addressBook.setUserId(userId);
        addressBookMapper.insert(addressBook);
        log.info("用户id:{}更新数据完成", userId);
    }

    /**
     * 根据id主键删除用户地址信息
     *
     * @param id
     */
    public void deleteById(Long id) {
        AddressBook addressBook = new AddressBook();
        addressBook.setId(id);
        addressBookMapper.deleteById(addressBook);
    }

    /**
     * 根据id主键设置默认地址
     *
     * @param addressBook
     */
    @Transactional//事务同步
    public void setDefaultAddressBook(AddressBook addressBook) {
        //先根据userId查询 将所有的is_default更新为0 再进行更新所传入的id
        Long userId = BaseContext.getCurrentId();
        addressBook.setUserId(userId);
        log.info("用户id{}:清除默认地址", userId);
        addressBookMapper.deleteDefault(addressBook);
        log.info("用户id{}:更新默认地址", userId);
        addressBookMapper.updateDefault(addressBook);
    }

}
